//
//  UIView+Masonry_LJC.m
//  cs4
//
//  Created by 徐汉卿 on 15/6/17.
//  Copyright (c) 2015年 徐汉卿. All rights reserved.
//xx

#import "UIView+Masonry_LJC.h"
//#import "Masonry.h"
@implementation UIView (Masonry_LJC)

- (void) distributeSpacingVerticallyWith:(NSArray*)views
{
    
#warning xhq修改
//    for (UIView *view in self.subviews) {
//        
////        if (view.tag ==123) {
////            [view removeFromSuperview];
////        }
//    }
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( int i = 0 ; i <= views.count ; i++ )
    {
        UIView *v = [UIView new];
        v.tag = 123;
//        v.backgroundColor = [UIColor purpleColor];
        [spaces addObject:v];
        [self addSubview:v];
        
        [v mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(v.mas_height);
            make.centerX.equalTo(self);
        }];
    }
//    NSLog(@"xxxx%ld",spaces.count);
    UIView *spaceCurrent = nil;
    
    UIView *objCurrent = nil;
    
    for (int i = 0; i <= views.count; i++) {
        
        
        
        spaceCurrent = spaces[i];
        
        
        
        if (i > 0) {
            UIView *spaceShangYiGe = spaces[i -1];
            [spaceCurrent mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(spaceShangYiGe);
            }];
        }
        if (i <views.count) {
            objCurrent = views[i];
            [spaceCurrent mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(objCurrent.mas_top);
            }];
        }else{
            [spaceCurrent mas_makeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self);
            }];
        }
        
        
        if (i == 0) {
            [spaceCurrent mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self);
            }];
        }else{
            UIView *objShangYiGe = views[i -1];
            [spaceCurrent mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(objShangYiGe.mas_bottom);
            }];
        }
        
        
    }
    
    
    
    
}






- (void) distributeSpacingHorizontallyWith:(NSArray*)views
{
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( int i = 0 ; i < views.count+1 ; ++i )
    {
        UIView *v = [UIView new];
        v.backgroundColor =[UIColor yellowColor];//RGBCOLORA(1, 0, 0, 0.5);

        [spaces addObject:v];
        [self addSubview:v];
        //新建数组+1个view 放到新数组，设置view 等宽
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(v.mas_height);
        }];
    }
    
    UIView *v0 = spaces[0];
    
    __weak __typeof(&*self)ws = self;
    //新数组的第一个view0 左对齐父视图，yCenter 与 旧数组里的view0 相同
    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.mas_left);
        make.centerY.equalTo(((UIView*)views[0]).mas_centerY);
    }];
    //新建lastSpace  指针 指向新数组的view0
    UIView *lastSpace = v0;
    for ( int i = 0 ; i < views.count; ++i )
    {

        UIView *obj = views[i];
        UIView *space = spaces[i+1];
        //旧数组   view左边对齐  新数组 相同坐标 view右
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastSpace.mas_right);
        }];
        //新数组 坐标+1的view 左对齐 旧数组 的view的右，centerY 对齐旧数组  ， 宽度等同于新数组的第一项
        [space mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(obj.mas_right);
            make.centerY.equalTo(obj.mas_centerY);
            make.width.equalTo(v0);
        }];
        //lastSpace  指针 指向新数组的下一个对象
        lastSpace = space;
    }
    //新数组最后1个view 右对齐父视图
    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(ws.mas_right);
    }];
    
}

//- (void) distributeSpacingVerticallyWith:(NSArray*)views
//{
//    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
//    
//    for ( int i = 0 ; i < views.count+1 ; ++i )
//    {
//        UIView *v = [UIView new];
//        [spaces addObject:v];
//        [self addSubview:v];
//        
//        [v mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.equalTo(v.mas_height);
//        }];
//    }
//    
//    
//    UIView *v0 = spaces[0];
//    
//    __weak __typeof(&*self)ws = self;
//    
//    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(ws.mas_top);
//        make.centerX.equalTo(((UIView*)views[0]).mas_centerX);
//    }];
//    
//    UIView *lastSpace = v0;
//    for ( int i = 0 ; i < views.count; ++i )
//    {
//        UIView *obj = views[i];
//        UIView *space = spaces[i+1];
//        
//        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(lastSpace.mas_bottom);
//        }];
//        
//        [space mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(obj.mas_bottom);
//            make.centerX.equalTo(obj.mas_centerX);
//            make.height.equalTo(v0);
//        }];
//        
//        lastSpace = space;
//    }
//    
//    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(ws.mas_bottom);
//    }];
//    
//}




//
//-(void)xhqHorizontallyWith:(NSArray*)views
//{
//     __weak __typeof(&*self)ws = self;
//    NSMutableArray *arrayM = [NSMutableArray array];
//    for (int i=0; i <2; i++) {
//        UIView *tView =[UIView new];
//        [arrayM addObject:tView];
//        [self addSubview:tView];
//        [tView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.centerY.equalTo(ws);
//            make.width.equalTo(@(tView.height));
//        }];
//    }
//    
//    
//    UIView *pTView =arrayM[0];
//    for (int i = 0; i <arrayM.count; i++) {
//        UIView *viewTempT =arrayM[i];
//        [viewTempT mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.equalTo(pTView);
//        }];
//        
//    }
//    
//    //旧的数组组对齐self
//    UIView *view0 =views[0];
//    [view0 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(ws);
//    }];
//    //新旧数组 交互
//    UIView *lastSpace = view0;
//    for ( int i = 0 ; i < arrayM.count; ++i )
//    {
//
//        UIView *old = views[i+1];
//        UIView *new = arrayM[i];
//        //旧数组   view左边对齐  新数组 相同坐标 view右
//        [new mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(lastSpace.mas_right);
//        }];
//        //新数组 坐标+1的view 左对齐 旧数组 的view的右，centerY 对齐旧数组  ， 宽度等同于新数组的第一项
//        [old mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(new.mas_right);
//            make.centerY.equalTo(new.mas_centerY);
//            make.width.equalTo(view0);
//        }];
//        //lastSpace  指针 指向新数组的下一个对象
//        lastSpace = old;
//    }
//    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(ws);
//    }];
//
//    //mas_updateConstraints
//}

-(void)xhqHorizontallyWith:(NSArray*)views
{
    __weak __typeof(&*self)ws = self;
    NSMutableArray *arrayM = [NSMutableArray array];
    for (int i=0; i <2; i++) {
        UIView *tView =[UIView new];
        tView.backgroundColor =[UIColor blueColor];
        [arrayM addObject:tView];
        [self addSubview:tView];
        [tView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(ws);
            make.width.equalTo(@(tView.height));
        }];
    }
    
    
    UIView *pTView =arrayM[0];
    for (int i = 0; i <arrayM.count; i++) {
        UIView *viewTempT =arrayM[i];
        [viewTempT mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(pTView);
        }];
        
    }
    
    //旧的数组组对齐self
    UIView *view0 =views[0];
    [view0 mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws);

    }];
    //新旧数组 交互
    UIView *lastSpace = view0;
    for ( int i = 0 ; i < arrayM.count; ++i )
    {
        
        UIView *old = views[i+1];
        UIView *new = arrayM[i];
        //旧数组   view左边对齐  新数组 相同坐标 view右
        [new mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastSpace.mas_right);
        }];
        //新数组 坐标+1的view 左对齐 旧数组 的view的右，centerY 对齐旧数组  ， 宽度等同于新数组的第一项
        [old mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(new.mas_right);
            make.centerY.equalTo(new.mas_centerY);
            make.width.equalTo(view0);
        }];
        //lastSpace  指针 指向新数组的下一个对象
        lastSpace = old;
    }
    [lastSpace mas_updateConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(ws);
    }];
    
    //mas_updateConstraints
}


- (void) xhqdistributeSpacingHorizontallyWith2:(NSArray*)views
{
    NSMutableArray *spaces = [NSMutableArray arrayWithCapacity:views.count+1];
    
    for ( int i = 0 ; i < views.count+1 ; ++i )
    {
        UIView *v = [UIView new];
        v.backgroundColor = [UIColor yellowColor];
        [spaces addObject:v];
        [self addSubview:v];
        //新建数组+1个view 放到新数组，设置view 等宽
        [v mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(v.mas_height);
        }];
    }
    
    UIView *v0 = spaces[0];
    
    __weak __typeof(&*self)ws = self;
    //新数组的第一个view0 左对齐父视图，yCenter 与 旧数组里的view0 相同
    [v0 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(ws.mas_left);
        make.centerY.equalTo(((UIView*)views[0]).mas_centerY);
    }];
    //新建lastSpace  指针 指向新数组的view0
    UIView *lastSpace = v0;
    for ( int i = 0 ; i < views.count; ++i )
    {
        UIView *obj = views[i];
        UIView *space = spaces[i+1];
        //旧数组   view左边对齐  新数组 相同坐标 view右
        [obj mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastSpace.mas_right);
        }];
        //新数组 坐标+1的view 左对齐 旧数组 的view的右，centerY 对齐旧数组  ， 宽度等同于新数组的第一项
        [space mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(obj.mas_right);
            make.centerY.equalTo(obj.mas_centerY);
            make.width.equalTo(v0);
        }];
        //lastSpace  指针 指向新数组的下一个对象
        lastSpace = space;
    }
    //新数组最后1个view 右对齐父视图
    [lastSpace mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(ws.mas_right);
    }];
    
}


//现在是 只考虑水平方向的限制
//- (void) xhq666distributeSpacingHorizontallyWith:(NSArray*)views
//{
//    //创建占位视图数组
//    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:views.count +1];
//    for (int i  = 0 ; i <views.count+1; i++) {
//        UIView *v = [UIView new];
//                v.backgroundColor = [UIColor yellowColor];
//        [self addSubview:v];
//        
//        [arrayM addObject:v];
//        
//    }
//    UIView *tempView = arrayM[0];
//    
//    //要让arrayM里的视图的宽度相同
//    for (int i= 0; i <arrayM.count; i++)
//    {
//        UIView *viewSub = arrayM[i];
//        [viewSub mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.width.equalTo(tempView.mas_width);
//            //            make.height.equalTo(tempView.mas_width);
//            //新数组 view 右边限制
//            if (i!=views.count)
//            {
//                UIView *viewRed =views[i];
//                make.right.equalTo(viewRed.mas_left);
//            }
//            else
//            {
//                make.right.equalTo(self.mas_right);
//            }
//            //新数组 view 左边限制
//            if (i==0)
//            {
//                make.left.equalTo(self.mas_left);
//            }
//            else
//            {
//                UIView *viewRed =views[i-1];
//                make.left.equalTo(viewRed.mas_right);
//                
//            }
//            
//        }];
//    }
//    
//    
//    
//}
/**
 *  水平等距，中间加弹簧
 *
 *
 */
- (void)xhq666distributeSpacingHorizontallyWith:(NSArray*)views
{
    //创建占位视图数组
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:views.count -1];
    for (int i  = 0 ; i <views.count-1; i++) {
        UIView *v = [UIView new];
        v.backgroundColor = [UIColor yellowColor];
        [self addSubview:v];
        
        [arrayM addObject:v];
        
    }
    UIView *tempView = arrayM[0];
    

    
        //要让arrayM里的视图的宽度相同
        for (int i= 0; i <arrayM.count; i++)
        {
            UIView *viewSub = arrayM[i];
            [viewSub mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(tempView.mas_width);
                //            make.height.equalTo(tempView.mas_width);
                //新数组 view 右边限制
                UIView *redViewL =views[i];
                UIView *redViewR = views[i+1];
                make.left.equalTo(redViewL.mas_right);
                make.right.equalTo(redViewR.mas_left);
            }];

        }
    for (int i=0; i <views.count; i++) {
        if (i==0) {
            UIView *redView =views[i];
            [redView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self);
            }];
        }
        else if(i==views.count-1)
        {
            UIView *redView =views[i];
            [redView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self);
            }];

        }
    }
    
}
-(void)xhqdistributeSpacingHorizontallyWith:(NSArray*)views leftMargin:(CGFloat)leftmarg rightPadding:(CGFloat)rightPadding{
    //创建占位视图数组
    NSMutableArray *arrayM = [NSMutableArray arrayWithCapacity:views.count -1];
    for (int i  = 0 ; i <views.count-1; i++) {
        UIView *v = [UIView new];
        v.backgroundColor = [UIColor yellowColor];
        [self addSubview:v];
        
        [arrayM addObject:v];
        
    }
    UIView *tempView = arrayM[0];
    
    
    
    //要让arrayM里的视图的宽度相同
    for (int i= 0; i <arrayM.count; i++)
    {
        UIView *viewSub = arrayM[i];
        [viewSub mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(tempView.mas_width);
            //            make.height.equalTo(tempView.mas_width);
            //新数组 view 右边限制
            UIView *redViewL =views[i];
            UIView *redViewR = views[i+1];
            make.left.equalTo(redViewL.mas_right);
            make.right.equalTo(redViewR.mas_left);
        }];
        
    }
    for (int i=0; i <views.count; i++) {
        if (i==0) {
            UIView *redView =views[i];
            [redView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(leftmarg);
            }];
        }
        else if(i==views.count-1)
        {
            UIView *redView =views[i];
            [redView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self).offset(-rightPadding);
            }];
            
        }
    }
    
}


@end
